<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Lists extends Allow
{
	//权限列表
    public function getindex()
    {
        $request=request();
        //获取需要修改的值
        $name=$request->param('name');
    	//获取权限node表所有信息
    	$data=Db::table('node')->where('name','like',"%".$name."%")->paginate(5);
    	//加载权限模板
    	return $this->fetch("Lists/index",['data'=>$data,'request'=>$request->param()]);
    	
    }

    //权限添加
    public function getadd(){
    	return $this->fetch("Lists/add");
    }

    //执行权限添加
    public function postinsert(){
        $request=request();
        //获取需要添加的值
        $node=$request->only(['name','mname','aname']);
        $node['status']=0;
        //判断值是否为空
        if(!empty($node['name']) && !empty($node['mname']) && !empty($node['aname'])){
            if(Db::table('node')->insert($node)){
                $this->success("添加成功","/adminlists/index");
            }else{
                $this->error("添加失败","/adminlists/add");
            }
        }else{
            $this->error("添加权限值不能为空","/adminlists/add");
        }
    }

    //删除
    public function getdelete(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //执行删除
        if(Db::table('node')->where('id',"{$id}")->delete()){
            $this->success("删除成功","/adminlists/index");
        }else{
            $this->error("删除失败","/adminlists/index");
        }
    }

    //修改
    public function getedit(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //查询需要修改的字段的信息
        $node=Db::table('node')->where('id',"{$id}")->find();
        return $this->fetch("Lists/edit",['node'=>$node]);
    }

    public function postupdate(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取需要修改的信息
        $data=$request->only(['name','mname','aname']);
        if(Db::table('node')->where('id',"{$id}")->update($data)){
            $this->success("修改成功","/adminlists/index");
        }else{
            $this->error("修改失败","/adminlists/edit");
        }
    }
}
